package net.qhzw.vip.qiyu.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyStringUtils {

    public static void main(String[] args) {
        String maxCode = "PQ202203110010";
        //String maxCode = "PQ2022031110010";
        //String maxCode = "PQ202203110010";
        String serlNum2 = MyStringUtils.getSerlNum2(maxCode);
        System.out.println(serlNum2);
    }



    public static String getSerlNum2(String maxCode) {
        String result = "";
        String timeStr = getTimeStr();
        if (maxCode != null && maxCode.contains(getTimeStr())) {
            String uid_end = maxCode.substring(getTimeStr().length(), maxCode.length());
            //吧String类型00011 转化为int的1
            int endNum = Integer.parseInt(uid_end);
            //结果为100002
            int tmpNum = 100000 + endNum + 1;
            //把100002首为1去掉
            result = timeStr + subStr("" + tmpNum, 1);
        } else {
            result = timeStr + "00001";
        }
        return result;
    }

    /*public static String getSerlNum2(String maxCode, String prefix) {
        String result = "";
        String timeStr = getTimeStr();
        if (maxCode != null && maxCode.contains(prefix)) {
            prefix = prefix + timeStr;
            String uid_end = maxCode.substring(11, maxCode.length());
            System.out.println("uid_end" + uid_end);
            //吧String类型00011 转化为int的1
            int endNum = Integer.parseInt(uid_end);
            //结果为100002
            int tmpNum = 100000 + endNum + 1;
            //把100002首为1去掉
            result = prefix + subStr("" + tmpNum, 1);
        } else {
            prefix = prefix + timeStr;
            result = prefix + "00001";
        }
        return result;
    }*/


    /****
     * 返回流水号 QY000001
     * @return
     */
    public static String getSerlNum(String maxCode) {
        String uidPfix = "QY";
        String commentCode = "";
        if (maxCode != null && maxCode.contains(uidPfix)) {
            String uid_end = maxCode.substring(2, maxCode.length());
            //吧String类型00011 转化为int的1
            int endNum = Integer.parseInt(uid_end);
            //结果为100002
            int tmpNum = 100000 + endNum + 1;
            //把100002首为1去掉
            commentCode = uidPfix + subStr("" + tmpNum, 1);
        } else {
            commentCode = uidPfix + "000001";
        }
        return commentCode;
    }

    /***
     * 生成流水号
     * @param maxCode
     * @param prefix
     * @return
     */
    public static String getSerlNum3(String maxCode, String prefix) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String timeStr = sdf.format(new Date());
        if (maxCode != null && maxCode.contains(prefix)) {
            prefix = prefix + timeStr;
            String uid_end = maxCode.substring(10, maxCode.length());
            System.out.println("uid_end:" + uid_end);
            //吧String类型00011 转化为int的1
            int endNum = Integer.parseInt(uid_end);
            //结果为100002
            int tmpNum = 10000 + endNum + 1;
            //把100002首为1去掉
            result = prefix + subStr("" + tmpNum, 1);
        } else {
            prefix = prefix + timeStr;
            result = prefix + "0001";
        }
        return result;
    }

    /***
     * 把1002的首尾1去掉的方法
     * @param str
     * @param start
     * @return
     */
    private static String subStr(String str, int start) {
        if (str == null || str.equals("")) {
            return "";
        }
        if (start < str.length()) {
            return str.substring(start);
        } else {
            return "";
        }
    }

    public static String getTimeStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(new Date());
    }
}
